﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Script.Serialization;

namespace MO
{
    public class ConfigAccess
    {
        static JavaScriptSerializer jser = new JavaScriptSerializer();
        static string runPath = null;
        /// <summary>
        /// 程序运行路径。
        /// </summary>
        public static string RunPath
        {
            get
            {
                if (runPath == null)
                {
                    runPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(ConfigAccess)).Location) + "\\";
                    if (runPath.Contains("Temporary ASP.NET Files"))
                    {
                        runPath = Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetAssembly(typeof(ConfigAccess)).CodeBase.Replace("file:///", ""))) + "\\";
                    }
                }
                return runPath;
            }
        }
        //= Path.GetDirectoryName(Assembly.GetAssembly(typeof(ConfigAccess)).CodeBase.Replace("file:///", "")) + "\\";
        static ConfigAccess()
        {
            //File.AppendAllText("c:\\tianyu\\1.txt", string.Format("{0}\r\n{1}\r\n{2}\r\n{3}\r\n", Assembly.GetAssembly(typeof(ConfigAccess)).Location,
            //    Assembly.GetAssembly(typeof(ConfigAccess)).CodeBase,
            //    ConfigPath,
            //    ConfigAccess.RunPath));

        }
        /// <summary>
        /// 配置文件路径。
        /// </summary>
        public static string ConfigPath { get { return RunPath + "config\\"; } }
        public static T Read<T>(string configName)
        {
            return jser.Deserialize<T>(File.ReadAllText(ConfigPath + configName, Encoding.UTF8));
        }
        public static void Save(string configName, object obj)
        {
            if (!Directory.Exists(ConfigPath))
                Directory.CreateDirectory(ConfigPath);
            File.WriteAllText(ConfigPath + configName, jser.Serialize(obj).Replace(",\"", ",\r\n \""), Encoding.UTF8);
        }
    }
    public class DBConfig
    {
        public const string ConfigName = "DBConfig.json";
        public DataBaseType DbType = DataBaseType.MySql;
        public string IP;
        public string Server;
        public string User;
        public string Password;
        public string GetConnection()
        {
            if (DbType == DataBaseType.Oracle)
                return string.Format("Data Source={0}/{1};User ID={2};Password={3};", IP, Server, User, Password);
            else if (DbType == DataBaseType.SqlServer)
                return string.Format("server = {0}; uid = {2} ;pwd = {3} ;database = {1}", IP, Server, User, Password);
            else
                return string.Format("Database='{1}';Data Source='{0}';User Id='{2}';Password='{3}';charset='utf8';pooling=true;Allow Zero Datetime=true;", IP, Server, User, Password);
        }
        public static DBConfig GetConfig()
        {
            if (!System.IO.File.Exists(ConfigAccess.ConfigPath + ConfigName))
            {
                ConfigAccess.Save(ConfigName, new DBConfig() { DbType = DataBaseType.MySql, IP = "127.0.0.1", Server = "modedb", User = "root", Password = "1234" });
            }
            return ConfigAccess.Read<DBConfig>(DBConfig.ConfigName);
        }
        public static string GetConnectionString()
        {
            return GetConfig().GetConnection();
        }
    }
    public enum DataBaseType
    {
        Oracle = 1,
        SqlServer = 2,
        MySql = 3
    }
}
